/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package myftp.app.action;

import java.io.IOException;
import myftp.app.model.ServerManagerModel;
import myftp.app.model.ServerModel;
import myftp.app.view.dialog.TemplateDialog;
import myftp.core.Template;
import myftp.core.action.AbstractAction;

/**
 *
 * @author CAVARLE Glenn, LUCAS Stéphane
 * Deconnect du serveur et ferme son onglet
 */
public class DeleteServerAction extends AbstractAction {

    public void run() {
        /* Demande la confirmation de la fermeture de l'onglet   */
        Integer response = TemplateDialog.showCloseTabDialog();


        if (response == TemplateDialog.YES_OPTION) {

            ServerManagerModel model = (ServerManagerModel) this.getModel();
            ServerModel server = model.getServer((Integer) this.getParam("serverIndex"));

            Integer tabIndex = model.getTabs().indexOf(server.getConnectIndex());
            Template layout = Template.getInstance();
            layout.getViewPart("tabContainer").remove(tabIndex);



            try {
                model.disconnect(server.getConnectIndex());


            } catch (InterruptedException ex) {
                TemplateDialog.showInternalErrorDialog(ex.getMessage());


            } catch (IOException ex) {
                TemplateDialog.showInternalErrorDialog(ex.getMessage());


            }
        }
    }

    public boolean init() {
        ServerManagerModel model = (ServerManagerModel) this.getModel();


        if (!model.hasServer((Integer) this.getParam("serverIndex"))) {
            return false;


        }
        return true;

    }
}
